package com.soda.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.mail.Session;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.soda.bean.News;
import com.soda.bean.User;
import com.soda.service.NewsService;
import com.soda.service.impl.NewsServiceImpl;
import com.soda.util.NewsExcel;
import com.soda.util.PageInfo;
import com.soda.util.SelectNewsInfo;
import com.soda.util.SelectUserInfo;

public class NewsServlet extends HttpServlet {
	NewsService ns = new NewsServiceImpl();
	News n = new News();
	

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String param = request.getParameter("param");// 获得参数，由参数判断执行方法
		if (param != null) {
			// 查看
			if ("selectAll".equals(param)) {
				if(request.getParameter("new")!=null){
					request.getSession().removeAttribute("sn");
				}
				selectAll(request, response);
			}
			// 删除
			if ("delete".equals(param)) {

				try {
					delete(request, response);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			// 编辑内容
			if ("edit".equals(param)) {

				edit(request, response);
			}
			if ("update".equals(param)) {
				update(request, response);
			}
		 
			
			// 增加
			if ("add".equals(param)) {
				add(request, response);
			}
			// 批量删除
			if ("alldelete".equals(param)) {
				alldelete(request, response);
			}
			//修改状态
			if ("updatestate".equals(param)) {
				updatestate(request, response);
			}
			
			if("show".equals(param)){
				try {
					show(request,response);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

	// 增加
	protected void add(HttpServletRequest request, HttpServletResponse response) {
		String title = request.getParameter("Name");
		String Author = request.getParameter("Author");
		int count = Integer.parseInt(request.getParameter("count"));
		int recommend = Integer.parseInt(request.getParameter("recommend"));
		String neirong = request.getParameter("neirong");
		HttpSession session = request.getSession();
		 
		News n = new News();
		n.setNewAuthor(Author);
		n.setNewTitle(title);
		n.setNewCount(count);
		n.setNewState(recommend);
		n.setNewContent(neirong);
		int num = ns.addNews(n);
		if (num > 0) {

			try {
				selectAll(request, response);
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
	 

	// 全查
	public void selectAll(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		NewsService ns = new NewsServiceImpl();
		// 防止乱码

		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");

		NewsService nes = new NewsServiceImpl();
		PageInfo pi = new PageInfo(1, 5, 0, 0, "NewsServlet", "selectAll");
		SelectNewsInfo sn = new SelectNewsInfo();
		// 从jsp中获取值
		String Title = request.getParameter("newTitle");
		String updateTime = request.getParameter("updateTime");
		String state = request.getParameter("select2");

		HttpSession session = request.getSession();
		if (session.getAttribute("sn") != null) {
			sn = (SelectNewsInfo) session.getAttribute("sn");
		} else {
			sn = new SelectNewsInfo("", "", -1);
		}

		if (Title != null) { // 分页条件赋值
			sn.setNewtitle(Title.length() == 0 ? "" : Title);

			sn.setUpdatetime(updateTime == null || updateTime.length() == 0 ? ""
					: updateTime);
			sn.setState(state.length() == 0 || state == null ? -1 : Integer
					.parseInt(state));
			request.getSession().setAttribute("sn", sn);
		} else {
			sn = (SelectNewsInfo) request.getSession().getAttribute("sn");
		}

		int page = 1;
		pi.setPer(5);// 分页信息赋值
		pi.setTotal(ns.getNewsCount(sn));

		String pages = request.getParameter("page");

		if (pages != null) {
			pi.setPage(Integer.parseInt(pages));
		}

		List<News> news = ns.getAllNews(sn, pi);
		request.setAttribute("news", news);
		session.setAttribute("page", pi);

		session.setAttribute("states", state);
		request.getRequestDispatcher("houtai/pages/newconter/index.jsp").forward(request, response);

	}

	// 单个删除
	protected void delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception, IOException {
		response.setContentType("text/html;charset=utf-8");
		int Id = Integer.parseInt(request.getParameter("newId"));

		if (ns.deleteById(Id) > 0) {
			selectAll(request, response);
		}

	}
	public void show(HttpServletRequest request,
			HttpServletResponse response) throws Exception, IOException {
		request.setCharacterEncoding("utf-8");
		int id = Integer.parseInt(request.getParameter("id"));
		List<News>list=ns.showNews(id);
		request.setAttribute("news", list);
		request.getRequestDispatcher("houtai/pages/newconter/info.jsp").forward(request, response);
	}
	// 批量删除
	protected void alldelete(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
			request.setCharacterEncoding("utf-8");
			String dellist = request.getParameter("id");
			String []list=dellist.split(",");
			int num = ns.delAllNews(list);
			PrintWriter out=response.getWriter();
			if (num > 0) {
				selectAll(request, response);
		 
		}
	}

	// 根据ID查信息
	protected void edit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		
		
		int id = Integer.parseInt(request.getParameter("editId"));
		News newss = ns.showAllNews(id);
		request.setAttribute("news", newss);
		request.getRequestDispatcher("houtai/pages/newconter/edit.jsp")
				.forward(request, response);

	}

	// 修改信息
	public void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		int id = Integer.parseInt(request.getParameter("Id"));
		int recommend = Integer.parseInt(request.getParameter("recommend"));
		String neirong = request.getParameter("neirong");

		int n = ns.updateNews(id, recommend, neirong);

		if (n > 0) {
			selectAll(request, response);

		} else {
			System.out.println("shibai");
		}
	}
	//修改状态
	public void updatestate(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		
		try {
			int state=Integer.parseInt(request.getParameter("state"));
			int newId=Integer.parseInt(request.getParameter("newid"));
			//更新
			int n=ns.updateStateNews(newId,state);
			PrintWriter out= response.getWriter();
			if(n>0){
				out.print("修改成功");
				//out.print("<script>location.href='NewsServlet?method='+'sel'</script>");
			}
			out.close();
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	
	
	
}
